package com.f1ussh.common.result;

import lombok.Data;

import java.io.Serializable;

/**
 * @Author: 马腾飞
 * @CreateTime: 2025-09-30
 * @Description: 标准接口响应结果
 * @Version: 1.0
 */

@Data
public class Result<T> implements Serializable {
    private Integer code;
    private String message;
    private T data;
    private Long timestamp;

    public Result() {
        this.timestamp = System.currentTimeMillis();
    }

    /**
     * 无结果返回
     * @return
     * @param <T>
     */
    public static <T> Result<T> success() {
        Result<T> result = new Result<T>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        return result;
    }

    /**
     * 带结果返回
     * @param data
     * @return
     * @param <T>
     */
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<T>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        result.setData(data);
        return result;
    }

    /**
     * 特殊描述返回成功
     * @param data
     * @param message
     * @return
     * @param <T>
     */
    public static <T> Result<T> success(T data, String message) {
        Result<T> result = new Result<T>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setData(data);
        result.setMessage(message);
        return result;
    }

    /**
     * 失败
     * @return
     * @param <T>
     */
    public static <T> Result<T> error() {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.ERROR.getCode());
        result.setMessage(ResultCode.ERROR.getMessage());
        return result;
    }

    /**
     * 特殊信息
     * @param message
     * @return
     * @param <T>
     */
    public static <T> Result<T> error(String message) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.ERROR.getCode());
        result.setMessage(message);
        return result;
    }

    /**
     * 失败特殊信息 特殊code
     * @param code
     * @param message
     * @return
     * @param <T>
     */
    public static <T> Result<T> error(Integer code, String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    /**
     * 参数异常，无信息
     * @return
     * @param <T>
     */
    public static <T> Result<T> validateError() {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.VALIDATE_ERROR.getCode());
        result.setMessage(ResultCode.VALIDATE_ERROR.getMessage());
        return result;
    }

    /**
     * 参数异常错误，带信息
     * @param message
     * @return
     * @param <T>
     */
    public static <T> Result<T> validateError(String message) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.VALIDATE_ERROR.getCode());
        result.setMessage(message);
        return result;
    }

    /**
     * 暂未登录或token已经过期
     * @return
     * @param <T>
     */
    public static <T> Result<T> unauthorized() {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.UNAUTHORIZED.getCode());
        result.setMessage(ResultCode.UNAUTHORIZED.getMessage());
        return result;
    }

    /**
     * 无权限访问
     * @return
     * @param <T>
     */
    public static <T> Result<T> forbidden() {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.FORBIDDEN.getCode());
        result.setMessage(ResultCode.FORBIDDEN.getMessage());
        return result;
    }

}
